package com.gitee.ice1938.module.admin.core;

import javax.sql.DataSource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.jdbc.AbstractBaseDataSourceInitializer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.gitee.ice1938.module.admin.core.operator.fun.OperatorService;
import com.gitee.ice1938.module.admin.framwork.AdminMenuFilter;
import com.gitee.ice1938.module.admin.framwork.DefaultMenuFilter;

/**
 * 默认admin支持功能
 * 
 * @author ice1938@qq.com
 */
@Configuration
@ComponentScan({ "com.gitee.ice1938.module.admin.core", "com.gitee.ice1938.module.admin.framwork" })
public class AdminCoreModuleConfig implements  CommandLineRunner,InitializingBean {

	@Autowired
	private OperatorService operatorService;
	@Autowired
	private MessageSource messageSource;

	/**
	 * 在APP启动后调用
	 */
	@Override
	@Transactional
	public void run(String... args) throws Exception {
		operatorService.initOperator();
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		// 根据模块信息添加i18n 默认配置 MessageSourceAutoConfiguration会根据是否配置了前缀进行资源相关类加载
		ResourceBundleMessageSource res = (ResourceBundleMessageSource) messageSource;
		res.addBasenames("com/gitee/ice1938/module/admin/core/operator/messages");
	}

	@Bean(name = "menuFilterRegistration")
	@ConditionalOnMissingBean
	public FilterRegistrationBean<AdminMenuFilter> menuFilterRegistration(AdminMenuFilter adminMenuFilter) {
		FilterRegistrationBean<AdminMenuFilter> filterRegistration = new FilterRegistrationBean<AdminMenuFilter>();
		filterRegistration.setFilter(adminMenuFilter);
		// 判断targetFilterLifecycle属性是false还是true,,决定是否调用自定义类的init()、destry()方法;该值缺省为false,表示生命周期由SpringApplicationContext管理,设置为true则表示由ServletContainer管理
		// filterRegistration.addInitParameter("targetFilterLifecycle", "true");
		// filterRegistration.setEnabled(true);
		// filterRegistration.setDispatcherTypes(DispatcherType.REQUEST,DispatcherType.FORWARD);
		filterRegistration.addUrlPatterns("/admin/*");// 可以自己灵活的定义很多，避免一些根本不需要被处理的请求被包含进来
		filterRegistration.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,*.woff,*.woff2");

		return filterRegistration;
	}

	@Bean
	@ConditionalOnMissingBean(AdminMenuFilter.class)
	public AdminMenuFilter defaultMenuFilter() {
		return new DefaultMenuFilter();
	}

	@Bean
	public AbstractBaseDataSourceInitializer initSysOptionsModuleDataSourceInitializer(DataSource dataSource,
	        ResourceLoader resourceLoader) {
		return new AbstractBaseDataSourceInitializer(dataSource, resourceLoader) {

			@Override
			protected String getSchemaLocation() {
				return "classpath:db/sys_operator_ddl.sql";
			}

			@Override
			protected String getValidSql() {
				return "select * from sys_operator;";
			}

		};

	}

	@Controller
	public class IndexController {

		/**
		 * 后台管理主页面
		 * 
		 * @return
		 */
		/*
		 * @RequestMapping({ "admin" }) public String index() { return
		 * "admin/index"; }
		 */
		@RequestMapping(path = { "admin", "/admin/{page}" }, method = RequestMethod.GET)
		public String pageIndex(@PathVariable(value = "page", required = false) String page) {
			if (page == null) {
				page = "index";
			}
			return "admin/" + page;
		}

	}

}
